package com.linxin.ai_automates_periodontal_treatment_system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.linxin.ai_automates_periodontal_treatment_system.common.Result;
import com.linxin.ai_automates_periodontal_treatment_system.dto.DoctorClassifyDto;
import com.linxin.ai_automates_periodontal_treatment_system.dto.PatientClassifyDto;
import com.linxin.ai_automates_periodontal_treatment_system.entity.DoctorClassify;
import com.linxin.ai_automates_periodontal_treatment_system.entity.PatientClassify;
import com.linxin.ai_automates_periodontal_treatment_system.service.PatientClassifyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/patientClassify")
public class PatientClassifyController {
    @Autowired
    private PatientClassifyService patientClassifyService;

    /**
     * 获取患者分类列表
     *
     * @param pageNum
     * @param pageSize
     * @param classifyName
     * @return
     */
    @GetMapping("/list")
    public Result<Page<PatientClassify>> list(
            @RequestParam(value = "pageNum", required = true) Integer pageNum,
            @RequestParam(value = "pageSize", required = true) Integer pageSize,
            @RequestParam(value = "classifyName", required = false) String classifyName
    ) {

        // 1. 创建分页查询对象
        Page<PatientClassify> page = new Page<>(pageNum, pageSize);


        // 2. 创建查询条件
        LambdaQueryWrapper<PatientClassify> wrapper = new LambdaQueryWrapper<>();
        if (classifyName != null && !classifyName.isEmpty()) {
            wrapper.like(PatientClassify::getClassifyName, classifyName);
        }

        // 根据id倒序
        wrapper.orderByDesc(PatientClassify::getId);

        // 3. 执行查询
        patientClassifyService.page(page, wrapper);

        return Result.success(page);
    }

    /**
     * 保存或更新患者分类
     *
     * @return
     */
    @PostMapping("/save")
    public Result<String> saveDoctorClassify(@RequestBody PatientClassifyDto patientClassifyDto) {
        // 1. 判断id是否为空，为空则新增，不为空则更新
        if (patientClassifyDto.getId() == null) {
            // 新增
            // 将dto转为entity
            PatientClassify patientClassify = new PatientClassify();
            BeanUtils.copyProperties(patientClassifyDto, patientClassify);
            // 设置跟更新和创建时间
            patientClassify.setCreateTime(LocalDateTime.now());
            patientClassify.setUpdateTime(LocalDateTime.now());
            patientClassifyService.save(patientClassify);
        } else {
            // 更新
            PatientClassify patientClassify = patientClassifyService.getById(patientClassifyDto.getId());
            BeanUtils.copyProperties(patientClassifyDto, patientClassify);
            // 设置更新时间
            patientClassify.setUpdateTime(LocalDateTime.now());
            patientClassifyService.updateById(patientClassify);
        }

        return Result.success("保存成功");
    }


    /**
     * 根据id删除患者分类
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result<String> deleteDoctorClassify(@PathVariable("id") Integer id) {
        patientClassifyService.removeById(id);
        return Result.success("删除成功");
    }

    /**
     * 获取所有患者分类列表
     */
    @GetMapping("/allList")
    public Result<List<PatientClassify>> allList(){
        // 1. 创建查询条件
        LambdaQueryWrapper<PatientClassify> wrapper = new LambdaQueryWrapper<>();
        // 2. 执行查询
        List<PatientClassify> doctorClassifyList = patientClassifyService.list(wrapper);
        return Result.success(doctorClassifyList);
    }
}
